package com.xiangmo.travel.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangmo.travel.Annotation.TokenRequired;
import com.xiangmo.travel.Dto.AttractionsCommentsDto;
import com.xiangmo.travel.Dto.AttractionsCommentsInfoDto;
import com.xiangmo.travel.Dto.PageInfoDto;
import com.xiangmo.travel.Dto.ResultData;
import com.xiangmo.travel.Entity.AttractionsComments;
import com.xiangmo.travel.Enums.CodeAndMsg;
import com.xiangmo.travel.Service.AttractionsCommentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论模块
 */
@Slf4j
@RestController
@RequestMapping("/api/attractions/comment")
public class AttractionsCommentsController {
    @Autowired
    private AttractionsCommentsService attractionsCommentsService;
    //添加评论
    @TokenRequired
    @PostMapping("/add")
    public ResultData<AttractionsComments> addComment(@RequestBody AttractionsCommentsDto attractionsCommentsDto,
                                                      @RequestHeader("Authorization") String token){
        if(attractionsCommentsService.addComment(attractionsCommentsDto) == 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), null, token.substring(7));
        }
        log.warn("景点评论添加异常！");
        return new ResultData<>(CodeAndMsg.COMMENT_ERROR.getCode(),
                CodeAndMsg.COMMENT_ERROR.getMessage(), null, token.substring(7));
    }
    //根据景点id 查询父评论
    @GetMapping("/page/{id}")
    public ResultData<PageInfoDto<AttractionsCommentsInfoDto>> getCommentPageByAttractionId(@PathVariable Long id,
                                                                              @RequestParam int pageNum,
                                                                              @RequestParam int pageSize){
        PageInfoDto<AttractionsCommentsInfoDto> page = attractionsCommentsService.getCommentByAttractionsId(id, pageNum, pageSize);
        if(page != null){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), page, null);
        }
        log.warn("根据景点id查询父评论异常!");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                CodeAndMsg.ERROR.getMessage(), null, null);
    }

    //登录后 根据景点id 查询父评论
    @TokenRequired
    @GetMapping("/page/{id}/{userId}")
    public ResultData<PageInfoDto<AttractionsCommentsInfoDto>> getCommentPageByAttractionIdAndUserId(@PathVariable Long userId, @PathVariable Long id,
                                                                                            @RequestParam int pageNum,
                                                                                            @RequestParam int pageSize, @RequestHeader("Authorization") String token){
        PageInfoDto<AttractionsCommentsInfoDto> page = attractionsCommentsService.getCommentByAttractionsIdAndUserId(userId, id, pageNum, pageSize);
        if(page != null){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), page, token.substring(7));
        }
        log.warn("根据景点id查询父评论异常!");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                CodeAndMsg.ERROR.getMessage(), null, token.substring(7));
    }

    //根据景点id 查询子评论
    @GetMapping("/page/child/{id}")
    public ResultData<PageInfoDto<AttractionsCommentsInfoDto>> getChildCommentPageByAttractionId(@PathVariable Long id,
                                                                                            @RequestParam int pageNum,
                                                                                            @RequestParam int pageSize){
        PageInfoDto<AttractionsCommentsInfoDto> page = attractionsCommentsService.getCommentByParentId(id, pageNum, pageSize);
        if(page != null){
            if(page.getPageTotal() == 0){
                return new ResultData<>(CodeAndMsg.CHILD_ERROR.getCode(),
                        CodeAndMsg.CHILD_ERROR.getMessage(), null, null);
            }
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), page, null);
        }
        log.warn("根据景点id查询子评论异常!");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                CodeAndMsg.ERROR.getMessage(), null, null);
    }

    //登录后 根据景点id 查询子评论
    @TokenRequired
    @GetMapping("/page/child/{id}/{userId}")
    public ResultData<PageInfoDto<AttractionsCommentsInfoDto>> getChildCommentPageByAttractionIdAndUserId(@PathVariable Long userId, @PathVariable Long id,
                                                                                                 @RequestParam int pageNum,
                                                                                                 @RequestParam int pageSize, @RequestHeader("Authorization") String token){
        PageInfoDto<AttractionsCommentsInfoDto> page = attractionsCommentsService.getCommentByParentIdAndUserId(userId, id, pageNum, pageSize);
        if(page != null){
            if(page.getPageTotal() == 0){
                return new ResultData<>(CodeAndMsg.CHILD_ERROR.getCode(),
                        CodeAndMsg.CHILD_ERROR.getMessage(), null, null);
            }
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), page, token.substring(7));
        }
        log.warn("根据景点id查询子评论异常!");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                CodeAndMsg.ERROR.getMessage(), null, token.substring(7));
    }

    //删除评论
    @TokenRequired
    @DeleteMapping("/delete/{id}")
    public ResultData<Page<AttractionsComments>> deleteCommentPageById(@PathVariable Long id,
                                                                       @RequestHeader("Authorization") String token){
        int status = attractionsCommentsService.deleteCommentById(id);
        if(status == 1){
            return new ResultData<>(CodeAndMsg.DELETE_SUCCESS.getCode(),
                    CodeAndMsg.DELETE_SUCCESS.getMessage(), null, token.substring(7));
        }
        log.warn("删除评论异常!");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                CodeAndMsg.ERROR.getMessage(), null, token.substring(7));
    }
    //回复评论
    @TokenRequired
    @PostMapping("/add/child")
    public ResultData<Page<AttractionsComments>> addChildComment(@RequestBody AttractionsCommentsDto attractionsCommentsDto,
                                                                 @RequestHeader("Authorization") String token){
        if(attractionsCommentsService.addChildComment(attractionsCommentsDto) == 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), null, token.substring(7));
        }
        log.warn("回复评论异常!");
        return new ResultData<>(CodeAndMsg.COMMENT_ERROR.getCode(),
                CodeAndMsg.COMMENT_ERROR.getMessage(), null, token.substring(7));
    }
}
